﻿<%
'''底层文件类,使用Sa来统筹
Class Ashapo_Fso_Class
	'内部使用Fso名称
	Private s_fso
	'内部使用的Stream组件名
	Private s_stm
	
	'''构造
	Private Sub Class_Initialize()
		Dim t_o : t_o = C_FSO_NAME
		Set s_fso = Server.CreateObject(t_o)
		s_stm = C_STREAM_NAME
	End Sub
	
	'''析构
	Private Sub Class_Terminate()
		Set s_fso = Nothing
	End Sub
	
	'''获取实际路径MapPath(不支持通配符)
	'p_p:路径
	Private Function absPath_(Byval p_p)
		On Error Resume Next
		If CStr(p_p) = "" Then
			absPath_ = "" : Exit Function
		End If
		If Mid(p_p,2,1)<>":" Then
			p_p = Server.MapPath(p_p)
		End If
		If Right(p_p,1) = "\" Then
			p_p = Left(p_p,Len(p_p)-1)
		End If
		absPath_ = p_p
		If Err.Number<>0 Then
			Response.Write(p_p)
			Response.End
		End If
	End Function

	'''判断文件是否存在
	'p_p:文件路径
	Public Function IsFile(Byval p_p)
		p_p = absPath_(p_p)
		IsFile = False
		If s_fso.FileExists(p_p) Then
			IsFile = True
		End If
	End Function
	
	'''判断服务器上的文件夹是否存在
	'p_p:文件夹路径
	Public Function IsFolder(Byval p_p)
		p_p = absPath_(p_p)
		IsFolder = False
		If s_fso.FolderExists(p_p) Then
			IsFolder = True
		End If
	End Function
	
	'''读取服务器端文件内容
	'p_p:文件路径
	Public Function ReadFile(Byval p_p)
		Dim t_s, t_t
		p_p = absPath_(p_p)
		If IsFile(p_p) Then
			Set t_s = Server.CreateObject(s_stm)
			With t_s
				.Type = 2
				.Mode = 3
				.Open
				.LoadFromFile p_p
				.Charset = "UTF-8"
				.Position = 2
				t_t = .ReadText
				.Close
			End With
			Set t_s = Nothing
			If RegTest(t_t,"^\uFEFF") Then
				t_t = RegReplace(t_t,"^\uFEFF","")
			End If
		Else
			t_t = ""
			'抛出错误
			SE(1)
		End If
		ReadFile = t_t
	End Function
	
	'''在服务器上创建文件夹
	'p_p:文件夹路径
	Public Function CreateFolder(Byval p_p)
		On Error Resume Next
		Dim t_p, t_ap, t_i
		CreateFolder = True
		p_p = absPath_(p_p)
		t_ap = Split(p_p,"\") : t_p = ""
		For t_i = 0 To Ubound(t_ap)
			t_p = t_p & t_ap(t_i) & "\"
			If Instr(t_p, absPath_("/") & "\")>0 Then
				If Not IsFolder(t_p) And t_i>0 Then
					s_fso.CreateFolder(t_p)
				End If
			End If
		Next
		If Err.Number<>0 Then
			CreateFolder = False
			'收集错误在Sa中进行
		End If
		Err.Clear()
	End Function
	
	'''在服务器端创建文件并写入内容
	'p_p:文件路径
	'p_c:写入内容
	'p_b:写入BOM头
	Public Function CreateFile(Byval p_p, Byval p_c, Byval p_b)
		On Error Resume Next
		Dim t_sm
		p_p = absPath_(p_p)
		CreateFile = CreateFolder(Left(p_p,InstrRev(p_p,"\")-1))
		If CreateFile Then
			Set t_sm = Server.CreateObject(s_stm)
			If p_b=1 Or p_b=True Then
				With t_sm
					.Type = 2
					.Open
					.Charset = "UTF-8"
					.Position = t_sm.Size
					.WriteText = p_c
					.SaveToFile p_p, 2
					.Close
				End With
			Else
				With t_sm
					.Type = 2
					.Mode = 3
					.Charset = "UTF-8"
					.Open
					.Writetext(p_c)
					.Position = 3
				End With
				Dim t_ns : Set t_ns = Server.CreateObject(s_stm)   
				With t_ns   
					.Mode = 3   
					.Type = 1   
					.Open()   
				End With 
				t_sm.CopyTo(t_ns)   
				t_ns.SaveToFile p_p, 2
				t_sm.Flush
				t_sm.Close
				Set t_ns = Nothing
			End If
			Set t_sm = Nothing
		End If
		If Err.Number<>0 Then
			CreateFile = False
			'收集错误在Sa中进行
		End If
		Err.Clear()
	End Function
	
	'''列出服务器上指定目录下所有的文件或文件夹
	'p_p:目录路径
	'p_t:指定类型(文件或文件夹或全部)
	Public Function [List](Byval p_p, Byval p_t)
		On Error Resume Next
		Dim t_f, t_fs, t_k, t_a(), t_i, t_l
		p_p = absPath_(p_p) : t_i = 0
		Select Case LCase(p_t)
		Case "1","file"
			t_l = 1
		Case "2","folder"
			t_l = 2
		Case Else
			t_l = 0
		End Select
		Set t_f = s_fso.GetFolder(p_p)
		If t_l = 0 Or t_l = 2 Then
			Set t_fs = t_f.SubFolders
			ReDim Preserve t_a(2, t_fs.Count-1)
			For Each t_k In t_fs
				t_a(0,t_i) = t_k.Name
				t_a(1,t_i) = t_k.DateLastModified
				t_a(2,t_i) = t_k.Type
				t_i = t_i + 1
			Next
		End If
		If t_l = 0 Or t_l = 1 Then
			Set t_fs = t_f.Files
			ReDim Preserve t_a(2, t_fs.Count+t_i-1)
			For Each t_k In t_fs
				t_a(0,t_i) = t_k.Name
				t_a(1,t_i) = t_k.DateLastModified
				t_a(2,t_i) = t_k.Type
				t_i = t_i + 1
			Next
		End If
		Set t_fs = Nothing
		Set t_f = Nothing
		[List] = t_a
		If Err.Number<>0 Then
			'抛出错误
			SE(5)
		End If
		Err.Clear()
	End Function
	
	'''获取文件名(不含后缀)
	'p_f文件名(可以包含路径)
	Public Function NameOfFile(Byval p_f)
		Dim t_re
		If IsN(p_f) Then
			NameOfFile = "" : Exit Function
		End If
		p_f = Replace(p_f,"\", "/")
		If Right(p_f,1) = "/" Then
			t_re = Split(p_f, "/")
			NameOfFile = IIF(p_t=0, t_re(Ubound(t_re)-1),"")
			Exit Function
		ElseIf Instr(p_f,"/")>0 Then
			t_re = Split(p_f,"/")(Ubound(Split(p_f,"/")))
		Else
			t_re = p_f
		End If
		If Instr(t_re,".")>0 Then
			NameOfFile = Left(t_re,InstrRev(t_re,".")-1)
		Else
			NameOfFile = t_re
		End If
	End Function
End Class
%>